package com.kaifamiao.month05.day09;

import org.jetbrains.annotations.NotNull;

import java.time.LocalTime;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class Demo04 {

    public static void main(String[] args) {

        ThreadPoolExecutor pool =new ThreadPoolExecutor(2, 10, 3L, TimeUnit.SECONDS,
                            new ArrayBlockingQueue<Runnable>(5),
                            new MyThreadFactory(),
                            new ThreadPoolExecutor.DiscardPolicy());

        for (int i = 0; i < 15; i++) {
            int num = i;
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        System.out.println(Thread.currentThread().getName() + " , 执行时间：" + LocalTime.now() + " , 编号：" + num);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
    }
}

class MyThreadFactory implements ThreadFactory {

    private final AtomicInteger atom = new AtomicInteger();

    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, "打工人" +  atom.addAndGet(1));
    }
}
